package com.ocom.payconfig.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.entity.card.EntityXfPointInfo;
import com.ocom.common.entity.payconfig.EntityMealPlan;
import com.ocom.common.vo.payconfig.OrderPlanMealVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoMealPlan extends BaseMapper<EntityMealPlan> {

//	@Select({"<script>",
//			"select M.*,P.point_name as pointName " +
//					"from meal_plan M " +
//					"left join xf_point_info P on M.com_id=P.com_id and M.point_id=P.point_id " +
//					"where M.com_id = #{comId} and M.merch_id = #{merchId} " +
//					"and M.start_date <= #{endDate} and M.stop_date >= #{beginDate} " +
//					" <when test=\"pointId !=null and  pointId > '0'\" > "+
//					"  and  M.point_id = #{pointId}  "+
//					" </when> "+
//					" order by M.start_date " +
//			"</script>"})

	@Select({"<script>",
			"select \n" +
					"M.*,P.point_name as pointName \n" +
					"from meal_plan M \n" +
					"left join xf_point_info P on M.com_id=P.com_id and M.point_id=P.point_id   \n" +
					"where M.com_id = #{comId} " +
					"<when test=\"merchId !=null and  merchId >'0'\" >" +
					"and M.merch_id = #{merchId} " +
					"</when>"+
					" and M.start_date &lt;= #{endDate} and M.stop_date &gt;= #{beginDate} " +
					"<when test=\"pointId !=null and  pointId >'0'\" > "+
					" and  M.point_id = #{pointId}  "+
					" </when> "+
					" order by M.start_date  " +
					"</script>"})
	public List<EntityMealPlan> mealPlanList(@Param("comId")Long comId,
											 @Param("merchId")Integer merchId,
											 @Param("endDate")String endDate,
											 @Param("beginDate") String beginDate,
											 @Param("pointId") Integer pointId);


	/**
	 * select  #{mealDate}  mealDate, m.meal_no,GROUP_CONCAT(
	 * distinct m.merch_id,':',m.point_id,':',m.receipt_id  SEPARATOR ';'
	 * 		) as InfoIds   from meal_plan p
	 * inner  join  meal_menu m on p.com_id=m.com_id and p.mealplan_id=m.mealplan_id
	 * where  p.com_Id =   #{comId}
	 * and   #{mealDate}   between p.start_date and p.stop_date
	 * and  m.days=MOD(DATEDIFF( #{mealDate} ,p.start_date) ,p.cycle_days)+1
	 * group by m.meal_no
	 */
	@Select({"<script>",
			"select \n" +
					"m.meal_no, \n" +
					"#{mealDate}  as mealDate,"+
					"GROUP_CONCAT(distinct m.merch_id,':',m.point_id,':',p.receipt_ids  SEPARATOR '#') as InfoIds  \n" +
					"from meal_plan p \n" +
					"inner  join  meal_menu m on p.com_id=m.com_id and p.mealplan_id=m.mealplan_id   \n" +
					"left join xf_meal_time x on x.com_id=m.com_id and m.meal_no=x.meal_no and x.acc_type_id=0  \n" +
					"where p.com_id = #{comId}  " +
					"and   #{mealDate} between p.start_date and p.stop_date "+
					"and  m.days=MOD(DATEDIFF( #{mealDate} ,p.start_date) ,p.cycle_days)+1  "+
					"and   UNIX_TIMESTAMP(now()) between  (UNIX_TIMESTAMP(#{mealDate})+x.allow_minute*60 -x.allow_days*24*3600) " +
					"and  (TIME_TO_SEC(x.begin_time)+ UNIX_TIMESTAMP(#{mealDate})-x.before_hour*3600)  " +
					" <when test=\"tagIds.size()>0 \" > "+
						"<foreach collection='tagIds' item='item' open=' and (p.tag_id  = \"\"  or p.tag_id  is null or p.tag_id = \"0\" or ' separator='or' close=')'>" +
							" FIND_IN_SET(#{item} , p.tag_id) > 0    "+
						"</foreach>"+
					" </when> "+
					" <when test=\"tagIds.size()==0 \" > "+
						" and (p.tag_id  = \"\"  or p.tag_id  is null or p.tag_id = \"0\" ) "+
					" </when> "+
					"<when test=\"pointId != null and  pointId &gt; 0\" >" +
					" and  p.point_id=#{pointId}" +
					" </when> "+
					"<when test=\"mealTimeCount != null and  mealTimeCount == 0\" >" +
					" and  x.point_id=0" +
					" </when> "+
					"<when test=\"mealTimeCount != null and  mealTimeCount &gt; 0\" >" +
					" and  x.point_id=#{pointId}" +
					" </when> "+
					"group by m.meal_no  " +
					"</script>"})
	public List<OrderPlanMealVo> getOrderPlanMeal(@Param("comId")Long comId,
												  @Param("mealDate")String mealDate,
												  @Param("tagIds")List<String> tagIds,
												  @Param("pointId") Integer pointId,
												  @Param("mealTimeCount") Integer mealTimeCount
												  );


	/**
	 * select distinct mp.merch_Id,mp.point_Id,xp.point_name ,xp.point_name ,xm.merch_name  from  meal_plan mp
	 * left join xf_point_info  xp on mp.com_id=xp.com_id and (mp.point_id =xp.point_id or mp.point_id =0)
	 * left join xf_merchant_info  xm on mp.com_id=xm.com_id and (mp.merch_id =xm.merch_id or mp.merch_id =0)
	 * where mp.com_Id = #{comId}  and mp.receipt_Idslike ‘%#{receiptId}%’
	 * and  #{mealDate }between mp.start_date and mp.stop_date
	 * order by mp.merch_Id,mp.point_Id
	 */
//	@Select({"<script>",
//			"select \n" +
//					"distinct mp.mealplan_id,mp.receipt_ids,mp.merch_id,mp.point_id,xp.point_name ,xp.point_name ,xm.merch_name \n" +
//					"from meal_plan mp \n" +
//					"left join xf_point_info  xp on mp.com_id=xp.com_id and (mp.point_id =xp.point_id or mp.point_id =0)   \n" +
//					"left join xf_merchant_info  xm on mp.com_id=xm.com_id and (mp.merch_id =xm.merch_id or mp.merch_id =0)  "+
//					"where mp.com_id = #{comId}  " +
//					"and  mp.receipt_ids is not null  and   find_in_set( #{receiptId}, mp.receipt_ids )    "  +
////					"and   #{mealDate}  between mp.start_date and mp.stop_date "+
//					"order by mp.merch_id,mp.point_id   " +
//					"</script>"})
	@Select({"<script>","" +
			" SELECT DISTINCT" +
			" xm.merch_id," +
			" xm.merch_name," +
			" xp.point_Id," +
			" xp.point_name," +
			" ifnull(pc.limit_type,0) as limitType"+
			" FROM" +
			" meal_plan mp" +
			" LEFT JOIN xf_point_info xp ON mp.com_id = xp.com_id" +
			" AND (" +
			" mp.point_id = xp.point_id" +
			" OR mp.point_id = 0" +
			" ) " +
			" LEFT JOIN xf_merchant_info xm ON mp.com_id = xm.com_id" +
			" AND (xp.merch_id = xm.merch_id)" +
			" LEFT JOIN point_config pc ON pc.com_id=mp.com_id AND pc.point_id=mp.point_id" +
			" WHERE" +
			" mp.com_Id = #{comId}" +
			"<when test=\"receiptId !=null and  receiptId >'0'\" > "+
			" AND find_in_set(#{receiptId}, mp.receipt_Ids) > 0" +
			"</when>" +
			"<when test=\"mealDate !=null \" > "+
			" and #{mealDate} between mp.start_date and mp.stop_date" +
			"</when>" +
			" ORDER BY" +
			" xm.merch_Id," +
			" xp.point_Id;" +
			"</script>"})
	public List<EntityMealPlan> getMerchPointList(@Param("comId")Long comId,
											 @Param("receiptId")Integer receiptId,
											 @Param("mealDate")String mealDate
											);

	@Select({"<script>","" +
			" select    M.point_id as pointId , P.point_name as pointName " +
			"  from      meal_plan as M , xf_point_info as P \n" +
			"  where      M.com_id = #{comId}      and      M.point_id = P.point_id      and      P.merch_id = #{merchId} \n" +
		//	"  and      M.start_date <= T+15      and      M.stop_date >= T+0 \n" +
			"  and      M.start_date <![CDATA[ <= ]]> #{stop}     and    M.stop_date <![CDATA[ >= ]]> #{start}  \n" +
			"  group by      M.point_id " +
			"  order by      P.point_name" +
			"</script>"})
	public List<EntityXfPointInfo> getMealPlanPointList(@Param("comId")Long comId,
														@Param("merchId")Integer merchId,
														@Param("start")String start,
														@Param("stop")String stop
														);
}


